# Replication Archive for
# Coppock, Alexander. Generalizing from Survey Experiments Conducted on Mechanical Turk: A Replication Approach
# Forthcoming in Political Science Research and Methods

rm(list = ls())

# Uncomment to set working directory to replication archive
# setwd("")

# Uncomment to install packages
# install.packages("tidyverse")
# install.packages("devtools")
# install.packages("knitr")
# devtools::install_github("acoppock/commarobust")

library(tidyverse)
library(commarobust)
library(knitr)

# load results and study metadata -----------------------------------------

load("coppock_generalizability_analysis_results.rdata")
load("coppock_generalizability_study_data.rdata")

results <-
  results %>%
  left_join(study_df) %>%
  left_join(dvs_df) %>%
  left_join(coefficients_df) %>%
  mutate(sample_factor = factor(
    sample,
    levels = c("original", "mt", "gfk"),
    labels = c("Original", "Mechanical Turk", "GfK")
  ))


# Define helper functions -------------------------------------------------

gen_entry <- function(est, se, p) {
  if (!is.na(p)) {
    entry <-
      paste0(format_num(est, digits = 2), " (", format_num(se, 2), ")")
    
    
    if (p < 0.05) {
      entry <- paste0(entry, "*")
    }
    return(entry)
  } else {
    return(NA)
  }
}

gen_entry_vec <- Vectorize(gen_entry)

format_num <- function(x, digits = 3) {
  x <- as.numeric(x)
  return(paste0(sprintf(paste0("%.", digits, "f"), x)))
}

# Make Table --------------------------------------------------------------

ests <-
  results %>%
  select(study_factor, coef_name, dv_name, sample, est) %>%
  spread(key = sample, value = est)

ses <-
  results %>%
  select(study_factor, coef_name, dv_name, sample, se) %>%
  spread(key = sample, value = se)

ps <- 
  results %>%
  select(study_factor, coef_name, dv_name, sample, p) %>%
  spread(key = sample, value = p)


table_df <-
  left_join(ests,
            ses,
            by = c("study_factor", "coef_name", "dv_name"),
            suffix = c("_est", "_se")) %>%
  left_join(ps) %>%
  arrange(dv_name) %>%
  arrange(study_factor) %>%
  mutate(original_entry = gen_entry_vec(original_est, original_se, original),
         mt_entry = gen_entry_vec(mt_est, mt_se, mt),
         gfk_entry = gen_entry_vec(gfk_est, gfk_se, gfk))

table_df <-
  within(table_df,{
    study_print_name <- study_factor
    study_print_name[duplicated(study_factor)] <- NA
  })


sink(file = "coppock_generalizability_table_A1.txt")
table_df %>%
  select(
    study_print_name,
    dv_name,
    coef_name,
    original_entry,
    mt_entry,
    gfk_entry
  ) %>%
  knitr::kable()
sink()


